/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author QuangHau
 */
public class WebsiteDAO {

    public static ArrayList<WebsiteDTO> SelectWebsite() {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_SelectDanhSachWebSite_All}");
                ResultSet rs = cs.executeQuery();
                ArrayList<WebsiteDTO> arr = new ArrayList<WebsiteDTO>();
                while (rs.next()) {
                    WebsiteDTO item = new WebsiteDTO();
                    item.setMaWebsite(rs.getInt("mawebsite"));
                    item.setTenWebsite(rs.getString("tenwebsite"));
                    arr.add(item);
                }
                con.close();
                return arr;
            } catch (SQLException ex) {
                Logger.getLogger(WebsiteDAO.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return null;
    }
    
    public static WebsiteDTO ThongTinWebsite(int maWebsite) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_ThongTinWebSite(?)}");
                cs.setInt("mawebsite", maWebsite);
                ResultSet rs = cs.executeQuery();
                WebsiteDTO website = new WebsiteDTO();
                if (rs.next()) {
                    website.setMaWebsite(rs.getInt("mawebsite"));
                    website.setTenWebsite(rs.getString("tenwebsite"));
                }
                con.close();
                return website;
            } catch (SQLException ex) {
                Logger.getLogger(WebsiteDAO.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return null;
    }

    public static boolean insertWebsite(WebsiteDTO websiteDTO) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call  usp_InsertWebSite(?)}");
                cs.setString("tenwebsite", websiteDTO.getTenWebsite());
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(WebsiteDTO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }
    
    public static boolean updateWebsite(WebsiteDTO websiteDTO) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call  usp_UpdateWebSite(?,?)}");
                cs.setInt("mawebsite", websiteDTO.getMaWebsite());
                cs.setString("tenwebsite", websiteDTO.getTenWebsite());
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(WebsiteDTO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }

    public static boolean DeleteWebsite(int maWebsite) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_DeleteWebSite(?)}");
                cs.setInt("mawebsite", maWebsite);
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(WebsiteDTO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }
}
